package cn.tedu.project.oa.sys.department.controller;


import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.sys.department.pojo.dto.AddDepartPara;
import cn.tedu.project.oa.sys.department.pojo.dto.EditDepartPara;
import cn.tedu.project.oa.sys.department.pojo.dto.QueryDepartPara;
import cn.tedu.project.oa.sys.department.pojo.vo.DepartmentVO;
import cn.tedu.project.oa.sys.department.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @description: 部门管理
 * @author: Miles
 */
@Api(tags = "04.部门管理")
@Slf4j
@RestController
@RequestMapping("/oa/department")
public class DepartmentController {

    @Autowired
    private DeptService deptService;

    /**
     * 新增部门
     * @param addDepartPara 部门信息
     * @return JsonResult
     */
    @ApiOperation(value = "新增部门")
    @PostMapping("admin/add")
    public JsonResult addDepartment(@Valid @RequestBody AddDepartPara addDepartPara) {
        log.debug("新增部门");
        deptService.addDepartment(addDepartPara);
        return JsonResult.ok();
    }

    /**
     * 删除部门
     * @param id 部门id
     * @return JsonResult 成功或者失败
     */
    @ApiOperation(value = "删除部门")
    @PostMapping("admin/delete/{id}")
    public JsonResult deleteDepartment(@PathVariable Long id) {
        log.debug("删除部门id {}", id);
        deptService.deleteDepartment(id);
        return JsonResult.ok();
    }

    /**
     * 修改部门
     * @param editDepartPara 部门信息
     * @return JsonResult 成功或者失败
     */
    @ApiOperation(value = "修改部门")
    @PostMapping("admin/update")
    public JsonResult EditDepartment(@Valid @RequestBody EditDepartPara editDepartPara) {
        log.debug("修改部门 editDepartPara : {}", editDepartPara);
        deptService.EditDepartment(editDepartPara);
        return JsonResult.ok();
    }

    /**
     * 获取部门列表
     * @param queryDepartPara 查询条件
     * @return JsonResult 部门列表
     */
    @ApiOperation(value = "获取部门列表")
    @GetMapping("list")
    public JsonResult getDepartmentList(QueryDepartPara queryDepartPara) {
        log.debug("获取部门列表 queryDepartPara : {}", queryDepartPara);
        List<DepartmentVO> departmentVOList = deptService.getDepartmentList(queryDepartPara);
        return JsonResult.ok(departmentVOList);
    }

    /**
     * 获取所有子部门
     * @param pid 上级id
     * @return
     */
    @ApiOperation(value = "获取所有子部门")
    @GetMapping("/list/children/{pid}")
    public JsonResult getDepartmentChildren(@PathVariable Long pid) {
        log.debug("获取所有子部门 id : {}", pid);
        List<DepartmentVO> departmentVOList = deptService.getDepartmentChildren(pid);
        return JsonResult.ok(departmentVOList);
    }

    /**
     * 获取部门总数
     */
    @ApiOperation("获取部门总数")
    @GetMapping("count")
    public JsonResult countDept(){
        log.debug("查询部门数");

        int num = deptService.count();

        return JsonResult.ok(num);
    }

    //TODO 部门树

    //TODO 获取子部门

}
